<?php


class FirstPage
{	
	/**
	*@var LoginValidation
	**/
	private $loginValidation;
	/**
	*@var string
	**/
	private static $username = "FirstPage::userName";
	/**
	*@var string
	**/
	private static $password = "FirstPage::password";

	/**
	*@var string
	**/
	private static $newMember = "newMember";
	/**
	*@var string
	**/
	private static $action = "login";

	/**
	*@var string css id
	**/
	private static $usernameTextField = "UsernameTextField"; 
	/**
	*@var string css id
	**/
	private static $formSubmitButton = "FormSubmitButton"; 
	/**
	*@var string css id
	**/
	private static $passwordTextField = "PasswordTextField";
	

	/**
	*@param LoginValidation
	**/
	public function __construct(LoginValidation $loginVal)
	{
		$this->loginValidation = $loginVal;
	}

	/**
	*@return string
	**/
	public function getFirstPage()
	{
		$body = $this->getFirstPageHeader();
		$body .= $this->getLoginForm();
		$body .= $this->getFirstPageFooter();

		return $body;
	}

	/**
	*@return string
	**/
	private function getFirstPageHeader()
	{
		$header = "<h1>Träningsdagboken!</h1>";
		return $header;

	}

	/**
	*@return string
	**/
	private function getLoginForm()
	{
		$username = $this->getUsername();
		$message;
		if($this->justCreatedNewMember())
		{
			$message = "<div id='SuccessMessageDiv'><p>Registreringen lyckades!</p></div>";
		}
		elseif($this->loginValidation->getErrorMessage() != "") 
		{
			
			$message = "<div id='ErrorMessageDiv'><p>". $this->loginValidation->getErrorMessage()."</p></div>";
			
		}
		
		return "
		<div id='loginformDiv' >
			<a href='RegisterMember.php'>Registrera ny användare</a>
			<form action='?".self::$action."' method='post' enctype='multipart/form-data'>
				<fieldset>
					$message
					<legend>Logga in - Användarnamn och Lösenord</legend>
					<label for='" . self::$usernameTextField . "' >Användarnamn :</label>
					<input type='text' size='20' name='" . self::$username . "' id='" . self::$usernameTextField . "' value='$username' />
					<label for='" . self::$passwordTextField . "' >Lösenord  :</label>
					<input type='password' size='20' name='" . self::$password . "' id='" . self::$passwordTextField . "' value='' />
					<input type='submit' name='' id='".self::$formSubmitButton."' value='Logga in' />
				</fieldset>
			</form>
		</div>";
	}

	/**
	*@return string
	**/
	public function getFirstPageFooter()
	{
		$footer = "<a>Robert Forsgren</a> <a>Träningsdagboken</a>" ;
		return $footer;
	}

	/**
	*@return boolean
	**/
	private function justCreatedNewMember()
	{
		if(isset($_GET[self::$newMember]))
		{
			return true;
		}
		return false;
	}

	/**
	*@return string POST
	**/
	public function getUsername()
	{
		if(isset( $_POST[self::$username]))
		{
			return $_POST[self::$username];
		}
		return "";
	}

	/**
	*@return string POST
	**/
	public function getPassword()
	{
		if(isset($_POST[self::$password]))
		{
			return $_POST[self::$password];
		}
		return "";
	}


}